﻿Type=Activity
Version=3.15
@EndOfDesignText@
#Region Module Attributes
	#FullScreen: False
	#IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Dim btnOK As Button
	Dim edtPassword As EditText
	Dim edtUser As EditText
	Dim spnUser As Spinner
	Dim lstZDRY As List
	Dim chbPass As CheckBox
	Dim chbOffLine As CheckBox
	
	Dim FSql As String
	Dim FUserCode,FUserPass,FUserName As String
	Dim FDBPass As Boolean
	Dim FOldSession As String
	
	Dim HttpClientTool As HttpClient
	
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("FormLogin")
	Activity.Title = "登录"	
	
	HttpClientTool.Initialize("HttpClientTool")
	
	InitLayOut
	
	InitData
	
	chbOffLine.Checked = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
	If UserClosed Then
		
	End If
	
End Sub


Sub btnOK_Click

	Dim sUser,sBM,sMC As String
	Dim iIndex As Int 
	
	sUser = edtUser.Text.Trim	
	iIndex = sUser.IndexOf(" ")
	If iIndex > 0 Then
		sBM = sUser.SubString2(0,iIndex)
	Else
		sBM = sUser
	End If
		
	If sBM="" Then
		ToastMessageShow("请输入或选择用户名!",False)
		edtUser.RequestFocus
		Return
	End If
	
	FUserCode = sBM
	FUserPass = edtPassword.Text
	
	PublicVar.GV_OffLogin = False
	
	If chbOffLine.Checked Then
		'离线登录
		UserOffLogin
	Else
		'登录远程服务器 
		UserLogin
	End If
	
	PublicVar.GV_UserCode = FUserCode
End Sub

'离线登录
Sub UserOffLogin As Boolean
	Dim sUser,sPass As String
	Dim DataSetRY As Cursor
	
	sUser = PublicVar.GV_DesTool.encrypt(FUserCode,PublicVar.GV_DesKey)
	If FDBPass = False Then
		FUserPass = PublicVar.GV_DesTool.encrypt(FUserPass,PublicVar.GV_DesKey)
	End If	
	sPass = FUserPass

	FSql = "SELECT BM,PASS2 FROM ZD_RY WHERE BM='"&FUserCode&"'"
	Log("SQL: " & FSql)
	DataSetRY = PublicVar.GV_SQLTool.ExecQuery(FSql)
	
	If DataSetRY.RowCount = 0 Then
		ToastMessageShow("登录失败"&CRLF &"还没在服务器登录过，无法进行离线登录", True)	
		DataSetRY.Close
	Else 
		DataSetRY.Position = 0
		If DataSetRY.GetString("PASS2") = FUserPass Then
			PublicVar.GV_OffLogin = True
			PublicVar.GV_Login = True
			
			AfterLogin
			
			Log("登录成功")
		Else
			ToastMessageShow("登录失败"&CRLF &"离线登录验证失败！", True)
			
		End If

		DataSetRY.Close
	End If
	
	
End Sub

Sub UserLogin As Boolean
	Dim sURL,sUser,sPass,sText As String
	Dim hrRequest As HttpRequest
	
	FOldSession = PublicVar.GV_UserSession
	
	PublicVar.GV_UserSession = ""
	
	sUser = PublicVar.GV_DesTool.Base64encrypt(FUserCode)
	If FDBPass = False Then
		FUserPass = PublicVar.GV_DesTool.Base64encrypt(FUserPass)
	End If	
	sPass = FUserPass
	
	sText = "User=" & sUser & "&PassWord=" & sPass
	 
	sURL = PublicVar.GetServerUrl&"/login"
	hrRequest.InitializePost2(sURL,sText.GetBytes("UTF8"))
	
	hrRequest.Timeout = PublicVar.GV_ConnTimeOut

	If HttpClientTool.Execute(hrRequest, 1) = False Then  Return 
	ProgressDialogShow("正在登录，请稍候...")	

End Sub

'初始化数据
Sub InitData
	If File.Exists(File.DirDefaultExternal, PublicVar.GV_ConfigDBFileName) = False Then
		PublicVar.GV_AppPath = DButils.CopyDBFromAssets(PublicVar.GV_ConfigDBFileName)
	End If

	PublicVar.GV_SQLTool.Initialize(PublicVar.GV_AppPath, PublicVar.GV_ConfigDBFileName, True)
	
	PublicVar.InitData_ServerConfig
	
	FDBPass = False
	
	InitData_RY

End Sub

Sub InitData_RY
	Dim DataSetRY As Cursor
	lstZDRY.Initialize
	
	FSql = "SELECT BM,MC,RQSJ,PASS,JZPASS FROM ZD_RY ORDER BY RQSJ DESC, BM"
	Log("SQL: " & FSql)
	DataSetRY = PublicVar.GV_SQLTool.ExecQuery(FSql)
	
	For i = 0 To DataSetRY.RowCount - 1
		DataSetRY.Position = i
		Dim RecMap As Map
		RecMap.Initialize
		RecMap.Put("BM",DataSetRY.GetString("BM"))
		RecMap.Put("MC",DataSetRY.GetString("MC"))
		RecMap.Put("RQSJ",DataSetRY.GetString("RQSJ"))
		RecMap.Put("PASS",DataSetRY.GetString("PASS"))
		RecMap.Put("JZPASS",DataSetRY.GetInt("JZPASS"))
		
		lstZDRY.Add(RecMap)
	Next
	DataSetRY.Close
	
	spnUser.Clear
	For i = 0 To lstZDRY.Size -1 
		RecMap = lstZDRY.Get(i)
		spnUser.Add(RecMap.Get("BM") & " " &RecMap.Get("MC"))
		
		If i =0 Then
			edtUser.Text = RecMap.Get("BM") & " " &RecMap.Get("MC")
			spnUser_ItemClick(0,edtUser.Text)
		End If
	Next

End Sub

Sub spnUser_ItemClick (Position As Int, Value As Object)
	Dim sUser,sBM,sMC As String
	Dim iIndex As Int
	Dim RecMap As Map
	
	edtUser.Text = Value
		
	sUser = edtUser.Text.Trim
	iIndex = sUser.IndexOf(" ")
	If iIndex > 0 Then
		sBM = sUser.SubString2(0,iIndex)
	Else
		sBM = sUser
	End If
	
	RecMap = GetUserRecMap(sBM)
	FUserName = RecMap.Get("MC")
	chbPass.Checked = RecMap.Get("JZPASS") = 1
	If chbPass.Checked Then
		edtPassword.Text = RecMap.Get("PASS")
		FDBPass = True
	End If
	
End Sub

Sub GetUserRecMap(AUser As String) As Map
	Dim RecMap As Map
	For i =0 To lstZDRY.Size -1
		RecMap = lstZDRY.Get(i) 
		If RecMap.Get("BM") = AUser Then
			Return RecMap	
		End If
	Next

End Sub

Sub InitLayOut
	Activity.AddMenuItem("选项","MenuItemLoginConfig")
	Activity.AddMenuItem("删除当前用户","MenuItemLoginConfig")
End Sub

Sub MenuItemLoginConfig_Click
	If Sender = "选项" Then
		StartActivity(LoginConfig)
	Else If Sender = "删除当前用户" Then
		DeleteUser
	End If
	
End Sub	

Sub HttpClientTool_ResponseSuccess(Response As HttpResponse, TaskId As Int)    
	Dim sResult,sSession,sUserName,sCode,sData As String
	Dim mapResult As Map
	
	ProgressDialogHide
	sResult = Response.GetString("UTF8").Trim
	
	Log(sResult)
	'获取Session
	PublicVar.GV_JSonTool.Initialize(sResult)
	mapResult = PublicVar.GV_JSonTool.NextObject
	
	sCode = mapResult.Get("Code")
	If sCode<>"0" Then
		sResult = mapResult.Get("Message")
		ToastMessageShow("登录失败"&CRLF &sResult, True)	
		Return
	End If
	
	sData = mapResult.Get("Data")
	sData = PublicVar.GV_DesTool.Base64decrypt(sData)
	PublicVar.GV_JSonTool.Initialize(sData)
	mapResult = PublicVar.GV_JSonTool.NextObject
	
	sSession = mapResult.Get("Session")
	
	If sSession="" Then
		ToastMessageShow("获取Session失败", True)
		PublicVar.GV_UserSession = FOldSession
		Return
	End If
	PublicVar.GV_UserSession = sSession
	
	sUserName = mapResult.Get("UserName")
	FUserName = sUserName
	
	'更新到本地设置
	UpdateRY
	
	PublicVar.GV_Login = True
	
	AfterLogin
	
	Log("登录成功")
End Sub
Sub HttpClientTool_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)   
	Dim sMsg As String
	ProgressDialogHide
	
	PublicVar.GV_UserSession = FOldSession
	
  Log(Reason)    
  Log(StatusCode) 
  sMsg = "登录失败."    
  If Reason <> Null Then sMsg = sMsg & CRLF & Reason    
  ToastMessageShow (sMsg, True)	
End Sub

Sub UpdateRY
	Dim iNow As Long
	
	DateTime.DateFormat = "yyyy-MM-dd"
	iNow = DateTime.Now
	
	PublicVar.GV_SQLTool.BeginTransaction
	Try
		'不存在的话，就插入
		FSql = "INSERT INTO ZD_RY(BM,MC) SELECT '"&FUserCode&"','"&FUserName&"' "& _
					 "WHERE NOT EXISTS(Select * FROM ZD_RY WHERE BM='"&FUserCode&"')  "
					 
		Log("SQL: " & FSql)
		PublicVar.GV_SQLTool.ExecNonQuery(FSql)
		
		FSql = "UPDATE ZD_RY SET RQSJ='"&DateTime.Date(iNow) & " "&DateTime.Time(iNow)&"',MC='"&FUserName&"' "
		FSql = FSql & ",PASS2='"&FUserPass&"' "
		If chbPass.Checked Then
			FSql = FSql & ",JZPASS=1,PASS='"&FUserPass&"' "
		Else 
		  FSql = FSql & ",JZPASS=0,PASS=Null "
		End If
		
		FSql = FSql & " WHERE BM='"&FUserCode&"' "
		Log("SQL: " & FSql)
		PublicVar.GV_SQLTool.ExecNonQuery(FSql)
	
		PublicVar.GV_SQLTool.TransactionSuccessful
	Catch
		Log(LastException.Message)
		ToastMessageShow (LastException.Message, True)	
	End Try
	PublicVar.GV_SQLTool.EndTransaction
End Sub
Sub edtUser_TextChanged (Old As String, New As String)
	If Old <> New Then
		edtPassword.Text = ""
		FDBPass = False
	End If
End Sub

Sub DeleteUser
	Dim sUser As String
	Dim iIndex As Int
	Dim RecMap As Map
	
	sUser = edtUser.Text.Trim	
	If sUser="" Then
	
		Return
	End If
	
	iIndex = sUser.IndexOf(" ")
	If iIndex > 0 Then
		sUser = sUser.SubString2(0,iIndex)
	End If
	
	edtUser.Text = ""
	edtPassword.Text = ""
	
	RecMap = GetUserRecMap(sUser)
	
	iIndex = spnUser.IndexOf(RecMap.Get("BM") & " " &RecMap.Get("MC"))
	If iIndex>-1 Then
		spnUser.RemoveAt(iIndex)
	End If
	
	FSql = "DELETE FROM ZD_RY WHERE BM='"&sUser&"' "
	Log("SQL: " & FSql)
	PublicVar.GV_SQLTool.ExecNonQuery(FSql)
	
	PublicVar.GV_UserCode = ""
	edtUser.RequestFocus
End Sub

Sub AfterLogin
	StartActivity("MainXT")  '主菜单界面

End Sub
Sub edtPassword_TextChanged (Old As String, New As String)
	If Old <> New Then
		FDBPass = False
	End If
End Sub